<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\Exception;

class DouyinOpen
{
    /* @var $httpClient HttpClient */
    private $httpClient;
    private $cacheClass;
    private $client_key = "awg1hfaffbac94e3";
    private $client_secret = "16bb95421d67fdbd4ffef97bcbbbde41";
    private $client_token = "";

    public function __construct($config = [])
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
        $this->client_key = $config['client_key'] ?? "awg1hfaffbac94e3";
        $this->client_secret = $config['client_secret'] ?? "16bb95421d67fdbd4ffef97bcbbbde41";
    }



    public function authClientToken()
    {
        $token = $this->getClientToken();
        if (!empty($token)) {
            $this->client_token = $token;

            return $this->client_token;
        }
        $url = "https://open.douyin.com/oauth/client_token/";
        $body = [
            'client_key' => $this->client_key,
            'client_secret' => $this->client_secret,
            'grant_type' => 'client_credential',
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, ['content-type' => 'application/json',]);

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['access_token']) && !empty($json['data']['access_token'])) {
            $this->client_token = $json['data']['access_token'];
            $this->setClientToken($json['data']['access_token'], $json['data']['expires_in'] - 100);
            return $this->client_token;
        }
    }

    public function getHeader()
    {
        return [
            'content-type' => 'application/json',
            'access-token' => $this->authClientToken()
        ];
    }

    public function getClientToken()
    {
        return $this->cacheClass->get("douyin_client_token_" . $this->client_key);
    }

    public function setClientToken($token, $expire = 7200)
    {
        $this->cacheClass->set("douyin_client_token_" . $this->client_key, $token, $expire);
    }

    public function saveAgent($agent_nickname, $agentId = 0)
    {
        $url = "https://open.douyin.com/api/match/v1/taskbox/save_agent/";

        $body = [
            'agent_id' => $agentId,
            'agent_nickname' => $agent_nickname,
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['agent_id'])) {
            return $json['data']['agent_id'];
        }
        return false;
    }

    //获取合作链接
    public function gen_agent_link($agent_id, $app_id,$task_id, $agency_talent_uid)
    {
        $url = 'https://open.douyin.com/api/match/v1/taskbox/gen_agent_link/';

        $body = [
            'agent_id' => $agent_id,
            'app_id' => $app_id,
            'task_id' => $task_id,
            'agency_talent_uid' => $agency_talent_uid,
        ];
        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);

        return $json['data'];
    }

    public function query_app_task_id($appid, $create_start_time, $create_end_time)
    {
        $url = 'https://open.douyin.com/api/match/v1/taskbox/query_app_task_id/';
        $body = [
            'appid' => $appid,
            'create_start_time' => $create_start_time,
            'create_end_time' => $create_end_time,
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json);
        if (isset($json['err_no']) && $json['err_no'] == 0) {
            return $json['data']['task_ids'];
        }

        return [];
    }


    public function query_task_info($appid, $query_params_type, $query_params_content, $page_no = 1, $page_size = 100)
    {
        $url = "https://open.douyin.com/api/match/v1/taskbox/query_task_info/";
        $body = [
            'appid' => $appid,
            'query_params_type' => $query_params_type,
            'query_params_content' => $query_params_content,
            'page_no' => $page_no,
            'page_size' => $page_size,
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['err_no']) && $json['err_no'] == 0) {
            return $json['data']['tasks'];
        }

        return [];
    }

    public function query_agency_video_sum_data($video_publish_start_time, $video_publish_end_time, $douyin_id, $app_id, $agent_id, $page_no, $page_size)
    {
        $url = 'https://open.douyin.com/api/match/v1/taskbox/query_agency_video_sum_data/';
        $body = [
            'video_publish_start_time' => $video_publish_start_time,
            'video_publish_end_time' => $video_publish_end_time,
            'douyin_id' => $douyin_id,
            'app_id' => $app_id,
            'agent_id' => $agent_id,
            'page_no' => $page_no,
            'page_size' => $page_size,
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['err_no']) && $json['err_no'] == 0) {
            return $json['data']['tasks'];
        }
        return [];
    }

    public function query_agency_video_daily_data($video_publish_start_time, $video_publish_end_time,$billing_date, $douyin_id = '', $app_id = '', $agent_id = '', $page_no = 1, $page_size = 100)
    {
        $url = "https://open.douyin.com/api/match/v1/taskbox/query_agency_video_daily_data/";
        $body = [
            'video_publish_start_time' => $video_publish_start_time,
            'video_publish_end_time' => $video_publish_end_time,
            'billing_date' => $billing_date,
            'page_no' => $page_no,
            'page_size' => $page_size,
        ];

        if (!empty($app_id)) {
            $body['douyin_id'] = $app_id;
        }

        if (!empty($douyin_id)) {
            $body['douyin_id'] = $douyin_id;
        }

        if (!empty($agent_id)) {
            $body['agent_id'] = $agent_id;
        }

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        var_dump($url, $body,$response->getBody()->getContents());
        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json);

        return $json;
    }

    public function get_agency_user_bind_record($agent_id, $douyin_id = "", $agency_talent_uid = "", $page_no = 1, $page_size = 100)
    {
        $url = 'https://open.douyin.com/api/match/v1/taskbox/get_agency_user_bind_record/';
        $body = [
            'agent_id' => $agent_id,
            'page_no' => $page_no,
            'page_size' => $page_size,
        ];
        if (!empty($douyin_id)) {
            $body['douyin_id'] = $douyin_id;
        }
        if (!empty($agency_talent_uid)) {
            $body['agency_talent_uid'] = $agency_talent_uid;
        }

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json);

        return $json;
    }

    public function change_user_bind_agent($douyin_id, $old_agent_id, $new_agent_id)
    {
        $url = "https://open.douyin.com/api/match/v1/taskbox/change_user_bind_agent/";
        $body = [
            'douyin_id' => $douyin_id,
            'old_agent_id' => intval($old_agent_id),
            'new_agent_id' => intval($new_agent_id),
        ];

        var_dump($body);
        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json);

        return $json;
    }


}